﻿//**********************************************************
//Copyright(C)2010 By 波特商网公司 版权所有。
//
//文件名：
//文件功能：网格控件默认设置
//
//创建标识：米海波 || 2011-04-12
//
//修改标识：
//修改描述：
//**********************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace Hmc.IM.Common.UserControls
{
    /// <summary>
    /// 自定义网格控件
    /// </summary>
    public class ucDataGridView:DataGridView
    {

        /// <summary>
        /// 是否绑定空行
        /// </summary>
        private bool IsBindRowNull = false;

        private Image backgroundImage = null;

        /// <summary>
        /// 背景图片
        /// </summary>
        [Browsable(true), Category("背景图片"), Description("获取或设置搜索结果为空行时GridView背景图片")]
        public Image SearchResultEmptyImage
        {
            get { return backgroundImage; }
            set { backgroundImage = value; base.Refresh(); }
        }

        private bool isSelectedEdit = true;

        /// <summary>
        /// 是否选中时进入编辑
        /// </summary>
        [Browsable(true), Category("动作"), Description("获取或设置选中可读写状态单元格时是否直接进入编辑状态")]
        private bool IsSelectedEdit
        {
            get { return isSelectedEdit; }
            set { isSelectedEdit = value; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public ucDataGridView()
        {
            //初始化属性及样式
            this.AutoGenerateColumns = false;
            this.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            this.GridColor = Color.FromArgb(224, 242, 255);
            this.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
            this.CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal;
            this.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.BackgroundColor = Color.White;
            this.MultiSelect = false;
            this.ReadOnly = true;
            this.RowHeadersVisible = false; //不显示行标题
            this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            this.ColumnHeadersHeight = 25;
            //this.CurrentCell.Selected = false;
            this.AllowUserToResizeRows = false;
            this.AllowUserToResizeColumns = true;
            this.AllowUserToAddRows = false;
            this.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(245, 246, 250);
            this.ColumnHeadersDefaultCellStyle.ForeColor = Color.FromArgb(20, 110, 172);
            this.ColumnHeadersDefaultCellStyle.Padding = new Padding(10, 0, 0, 0);
            this.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.True;
            this.RowsDefaultCellStyle.Padding = new Padding(10, 4, 0, 4);
            this.RowsDefaultCellStyle.WrapMode = DataGridViewTriState.True;
            this.RowsDefaultCellStyle.SelectionBackColor = Color.FromArgb(239, 252, 255);
            this.RowsDefaultCellStyle.SelectionForeColor = Color.FromArgb(50, 50, 50);
            this.RowTemplate.Height = 30;
            
        }

        /// <summary>
        /// 加载设置是否允许当前系统样式
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInvalidated(InvalidateEventArgs e)
        {
            
            this.EnableHeadersVisualStyles = true;
            this.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders;
            base.OnInvalidated(e);
        }

        /// <summary>
        /// 绘制DataGridView背景
        /// </summary>
        /// <param name="graphics">绘制对象</param>
        /// <param name="clipBounds">显示区域</param>
        /// <param name="gridBounds">列表区域</param>
        protected override void PaintBackground(Graphics graphics, Rectangle clipBounds, Rectangle gridBounds)
        {
            base.PaintBackground(graphics, clipBounds, gridBounds);
            if (backgroundImage != null && IsBindRowNull)
                graphics.DrawImage(backgroundImage, gridBounds);
            else if (IsBindRowNull)
            {
                Font font = new System.Drawing.Font("宋体", 9);
                string msg = "没有找到相应的数据！";
                Brush brush = new SolidBrush(Color.Black);
                graphics.DrawString(msg, font, brush, gridBounds.Width / 2 - (msg.Length * font.Size) / 2, gridBounds.Height / 2 - font.Height);
            }
        }

        /// <summary>
        /// 绑定时判断数据行是否等于零
        /// </summary>
        /// <param name="e"></param>
        protected override void OnDataBindingComplete(DataGridViewBindingCompleteEventArgs e)
        {
            base.OnDataBindingComplete(e);
            IsBindRowNull = this.Rows.Count > 0 ? false : true;
        }

        /// <summary>
        /// 进入编辑时设置输入法为半角
        /// </summary>
        /// <param name="e"></param>
        protected override void OnCellBeginEdit(DataGridViewCellCancelEventArgs e)
        {
            base.OnCellBeginEdit(e);
            this.ImeMode = System.Windows.Forms.ImeMode.Off;
        }

        /// <summary>
        /// 错误反馈
        /// </summary>
        /// <param name="displayErrorDialogIfNoHandler"></param>
        /// <param name="e"></param>
        protected override void OnDataError(bool displayErrorDialogIfNoHandler, DataGridViewDataErrorEventArgs e)
        {
            //base.OnDataError(displayErrorDialogIfNoHandler, e);
            if (displayErrorDialogIfNoHandler)
            {
                Type type = this.Columns[e.ColumnIndex].ValueType;
                if ((type == typeof(decimal)) || (type == typeof(double)) || (type == typeof(float)) || (type == typeof(int)))
                {
                    MessageBox.Show("数据格式不正确,只能输入数字或小数位的数字！", "格式错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    MessageBox.Show("数据格式不正确！", "格式错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                this.CancelEdit();
            }
        }

        /// <summary>
        /// 重写鼠标点击单元格事件，判断是否点击进入编辑
        /// </summary>
        /// <param name="e"></param>
        protected override void OnCellClick(DataGridViewCellEventArgs e)
        {
            base.OnCellClick(e);
            if ((e.RowIndex > -1) && !this.Columns[e.ColumnIndex].ReadOnly && this.IsSelectedEdit)
            {
                this.CurrentCell = this.Rows[e.RowIndex].Cells[e.ColumnIndex];
                this.BeginEdit(true);
            }
        }
        private bool isExistsError;

        /// <summary>
        /// 获取是否存在列表错误
        /// </summary>
        public bool IsExistsError
        {
            get
            {
                SearchError();
                return isExistsError;
            }
            set
            {
                isExistsError = value;
            }
        }

        private void SearchError()
        {
            int rowCount = this.Rows.Count;
            int colCount = this.Columns.Count;
            for (int i = 0; i < rowCount; i++)
            {
                for (int j = 0; j < colCount; j++)
                {
                    if (this.Rows[i].Cells[j].ErrorText != string.Empty)
                    {
                        this.IsExistsError = true;
                        goto Finish;
                    }
                    else
                    {
                        this.IsExistsError = false;
                    }
                }
            }
            Finish:
             System.Diagnostics.Debug.WriteLine("End of search.");
        }
    }
}
